package pers.vicxu.springbootdemo.interceptor;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import pers.vicxu.springbootdemo.dto.ResponseMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Create by Vic Xu on 2018/8/18
 */
@Component
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String name = (String) request.getSession().getAttribute("name");
        if (name != null) {
            return true;
        }
        ResponseMessage message = new ResponseMessage();
        message.setCode(200);
        message.setContent("LoginFailed");
        response.getWriter().write(JSON.toJSONString(message));
        return false;
    }
}
